// Company/index/index.js
const app = getApp(),
url = app.globalData.url;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    positionList:[],
    search:{
      pageNum: 1,
      pageSize: 10,
      enterprise_uid: "",
    },
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let userInfo = wx.getStorageSync('userInfo')
    this.data.search.enterprise_uid = userInfo.enterpriseUid
    console.log('地址参数',userInfo.enterpriseUid)
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getPosition();
  },
  //职位列表数据
  getPosition(){
    var that = this
    wx.request({
      url: url + 'enterprise/getPositionByEnterpriseIdShowAll',
      data: this.data.search,
      method: "GET",
      success:function(res){
        console.log('职位管理',res.data)
        if(res.data.code != 200 || !res.data.data){
          wx.showToast({
            title: res.data.msg,
            icon:'error'
          })
          return
        }
        let positionList = (res.data.data.content || []).filter(item => item.id)
        if(that.data.search.pageNum > 1 && positionList.length === 0){
          wx.showToast({
            title: '到底了~~',
            icon:'none'
          })
          return
        }
        positionList.forEach(item => item.checked = false)
        that.setData({
          positionList: [...that.data.positionList, ...positionList]
        })
      }
    })
  },
  //删除
  deletePosition(e){
    console.log(e)
    var id = e.currentTarget.dataset.id;
    const that = this
    wx.request({
      url: url + 'position/deletePosition?position_id='+ id,
      method: "DELETE",
      success:function(res){
        if(res.data.code == 200){
          wx.showToast({
            title: '删除成功',
            icon:'none'
          })
          that.getPosition()
        }else{
          wx.showToast({
            title: ret.msg,
            icon:'none'
          })
        }
      }
    })
  },
  //查看
  editPosition(e){
    console.log(e.currentTarget.dataset.id);
    wx.navigateTo({
      url: '/Company/newposition/newposition?id='+ e.currentTarget.dataset.id
    })
  },
  //开放or关闭
  openClosePosition(e){
    console.log(e)
    var id = e.currentTarget.dataset.id;
    var status = e.currentTarget.dataset.status;
    const that = this
    wx.request({
      url: url + 'position/updatePositionState',
      method: "PUT",
      data:{
        id: id, 
        recruitState: status
      },
      success:function(res){
        if(res.data.code == 200){
          wx.showToast({
            title: '成功',
            icon:'none'
          })
          that.getPosition()
        }else{
          wx.showToast({
            title: ret.msg,
            icon:'none'
          })
        }
      }
    })
  },
   //跳转发布新职位
   jumpNewzhiwei(e){
    let userInfo = wx.getStorageSync('userInfo')
    console.log(e)
    if(userInfo.approval == 1){
      wx.showToast({
        title: '企业信息审核中，暂不能增加职位',
        icon:'none',
      })
      return false
    }
    if(userInfo.approval == 3){
      wx.showToast({
        title: '企业信息未通过审核，不能增加职位',
        icon:'none',
      })
      return false
    }
    var url = e.currentTarget.dataset.url
    wx.navigateTo({
      url: url,
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /**
     * 生命周期函数--监听页面初次渲染完成
     */
  onReady: function () {
      
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
   onReachBottom: function () {
    this.data.search.pageNum++
    this.setData({
      search: this.data.search
    })
    this.getPosition()  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})